package com.by.common.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
@Component
public class CrosFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        /* 允许跨域的主机地址 */
        response.addHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        response.addHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        response.addHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        response.addHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        response.addHeader("Access-Control-Allow-Credentials", "false");
        chain.doFilter(request, response);
    }
}
